﻿$(document).ready(function () {
	drawLoadedReservesGrid();
});

function drawLoadedReservesGrid() {
	var $gridLoadedReserves = $("#gridLoadedReserves").kendoGrid({
		dataSource: {
			type: "aspnetmvc-ajax",
			transport: {
				read: {
					url: "Owner/getLoadedReserveList"
				}
			},
			error: function (e) {
				document.write(e.xhr.responseText);
			},
			schema: {
				model: {
					fields: {
						Reserve: { type: "object" }
					}
				},
				data: "Data",
				total: "Total",
				errors: "Errors"
			},
			group: "Date",
			serverPaging: true,
			serverSorting: true,
			serverGrouping: true,
			serverFiltering: true
		},
//		groupable: {
//			messages: {
//				empty: "Arrastre una columna para agrupar."
//			}
//		},
		height: 450,
		sortable: true,
		selectable: true,
		resizable: true,
		reorderable: true,
		filterable: true,
		pageable: {
			pageSize: 50,
			pageSizes: [10, 50, 100]
		},
		columns: [
            { field: "Time", title: "Hora" },
            { field: "Restaurant.Name", title: "Restaurante" },
            //{ field: "Restaurant.Address", title: "Direccion" },
            //{ field: "Restaurant.Telephone", title: "Telephone" },
            { field: "Restaurant.TableList[0].Description", title: "Mesa" },
			{ field: "Costumer.Name", title: "Cliente" },
			{ field: "Costumer.Telephone", title: "Telefono" },
			{ field: "Costumer.Mail", title: "Mail" },
			{ field: "Date", title: "Fecha", width: 1 },
			{ command: { text: "Confirmar", click: confirmReserve }, title: " ", width: "140px" }
		]
	}).data("kendoGrid");
}

function confirmReserve(e) {
	var answer = window.confirm("Desea confirmar la reserva?");

	if (answer == true) {
		e.preventDefault();

		var dataItem = this.dataItem($(e.currentTarget).closest("tr"));

		dataItem.State = 'Confirmada';

		updateReserve(dataItem);

		$("#gridLoadedReserves").data("kendoGrid").dataSource.read();
	}
}